package com.bawei.park2302.visit

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import androidx.activity.viewModels
import androidx.core.view.isVisible
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bawei.lib_base.utils.utils.entity.RepairBeanItem
import com.bawei.lib_base.utils.utils.entity.VisitBeanItem
import com.bawei.lib_base.utils.utils.mvi.BaseActivity
import com.bawei.lib_base.utils.utils.mvi.UiState
import com.bawei.lib_base.utils.utils.request.RequestType
import com.bawei.park2302.R
import com.bawei.park2302.databinding.ActivityVisitDetailBinding
import com.blankj.utilcode.util.ToastUtils
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch

@Route(path = "/app/VisitDetailActivity")
@AndroidEntryPoint
class VisitDetailActivity : BaseActivity<ActivityVisitDetailBinding,VisitIntent,VisitViewModel>() {
    override val binding: ActivityVisitDetailBinding by lazy {
        ActivityVisitDetailBinding.inflate(layoutInflater)
    }
    override val vm: VisitViewModel by viewModels<VisitViewModel>()
    @Autowired
    @JvmField
    var visitId = 0
    override fun initView() {
        ARouter.getInstance().inject(this)

        vm.sendIntent(VisitIntent.visitDetail(visitId))

        binding.visitdetailBtn.setOnClickListener {
            if(binding.visitdetailRg.checkedRadioButtonId==R.id.visitdetail_yes){
                vm.sendIntent(VisitIntent.visitApprove(visitId))
            }else{
                vm.sendIntent(VisitIntent.visitReject(visitId))
            }
        }
    }

    override fun initData() {
        lifecycleScope.launch {
            vm.uiState.collect{
                when(it){
                    is UiState.onSuccess<*> ->{
                        when(it.type){
                            RequestType.APPROVE ->{
                                ToastUtils.showLong("已通过")
                                finish()
                            }

                            RequestType.REJECT ->{
                                ToastUtils.showLong("已驳回")
                                finish()
                            }

                            RequestType.DETAIL ->{
                                val item = it.data as VisitBeanItem
                                binding.bean = item

                                if(item.visitStatus=="待审核"){
                                    binding.visitdetailGroup.visibility=View.VISIBLE
                                }
                            }
                            else -> {}
                        }
                    }
                    else -> {}
                }
            }
        }
    }
}